#include "Configuration.hpp"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
using std::ifstream;
using std::istringstream;
using std::cout;
using std::endl;

namespace wd{

Configuration* Configuration::_pinstance = nullptr;
Configuration::AutoRelease Configuration::_ar;

Configuration::Configuration(string filepath){
    ifstream infile(filepath);
    if(!infile){
        cout << "无法打开文件" << endl;
        exit(EXIT_FAILURE);
    }

    string line;
    while(std::getline(infile,line)){
        istringstream istring(line);
        string key,value;
        istring >> key;
        istring >> value;
        _map[key] = value;
    }

}

Configuration* Configuration::getInstance(const string & filepath){
        if(_pinstance == nullptr){
            _pinstance = new Configuration(filepath);
        }
        return _pinstance;
}

map<string,string> & Configuration::getConfigMap(){
    return _map;
}

}
#if 0
int main()
{
    wd::Configuration* config = wd::Configuration::getInstance("./server.conf");
    map<string,string> &config_map = config->getConfigMap();
    cout <<"ip:\t" <<config_map["ip"] << endl;
    cout <<"port:\t" <<config_map["port"] << endl;
    cout << "thread_num:\t"<< config_map["thread_num"]<< endl;
    cout << "task_num:\t"<<config_map["task_num"] << endl;
    cout << "video_path:\t"<<config_map["video_path"] << endl;

    return 0;
}
#endif
